package org.sippr.service.impl;

import org.sippr.mapper.*;
import org.sippr.pojo.*;
import org.sippr.service.CreationService;
import org.sippr.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class CreationImpl implements CreationService {
    @Autowired
    private CreationMapper creationMapper;
    @Autowired
    private CreationImgSketchMapper creationImgSketchMapper;
    @Autowired
    private CreationWordCueMapper creationWordCueMapper;
    @Autowired
    private CreationWordNegativeMapper creationWordNegativeMapper;
    @Autowired
    private CreationStyleMapper creationStyleMapper;
    @Autowired
    private SdCheckPointsMapper sdCheckPointsMapper;
    @Autowired
    private SdLorraMapper sdLorraMapper;
    @Autowired
    private TaskQueueMapper taskQueueMapper;
    @Autowired
    private CreationImgAiMapper creationImgAiMapper;

    @Override
    public void add(Creation value) {
        //获取登陆人id
        Map<String, Object> map = ThreadLocalUtil.get();
        String id = (String) map.get("id");
        value.setCreateUser(id);
        value.setModifyUser(id);

        //记录时间
        LocalDateTime time = LocalDateTime.now();
        value.setCreateTime(time);
        value.setModifyTime(time);
        value.setName(time.toString());
        //    0代表纯文字，1代表草图和文字
        value.setCate(0);
        Integer creationId = -1;
        //更新草图数据
        CreationImgSketch creationImgSketch = creationImgSketchMapper.findByName(value.getCreationImgSketch().getName());
        if (creationImgSketch != null) {
            creationImgSketch.setProjectId(value.getProjectId());
            //0代表纯文字，1代表草图和文字
            value.setCate(1);
            value.setSketchId(creationImgSketch.getId());
            creationImgSketchMapper.updateByPrimaryKey(creationImgSketch);

        }
        creationMapper.insert(value);
        creationId = value.getId();
        //新增正向提示词记录
        value.getCreationWordCue().setCreationId(creationId);
        creationWordCueMapper.insert(value.getCreationWordCue());

        //新增方向提示词记录
        value.getCreationWordNegative().setCreationId(creationId);
        creationWordNegativeMapper.insert(value.getCreationWordNegative());

        //新增创作参数
        CreationStyle creationStyle = value.getCreationStyle();
        creationStyle.setCreationId(creationId);
        SdLorra sdLorra = sdLorraMapper.selectByPrimaryKey(creationStyle.getLorraId());
        SdCheckPoints sdCheckPoints = sdCheckPointsMapper.selectByPrimaryKey(creationStyle.getCptId());
        creationStyle.setCptName(sdCheckPoints.getAlias());
        creationStyle.setLorraName(sdLorra.getAlias());
        creationStyleMapper.insert(creationStyle);

        //加入工作队列
        TaskQueue taskQueue = new TaskQueue();
        taskQueue.setCreationId(creationId);
        taskQueue.setState((short) 0);
        taskQueueMapper.insert(taskQueue);


    }

    @Override
    public Creation findById(Integer id) {
        return creationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Creation> findByProjectId(Integer projectId) {
        List<Creation> creationList = creationMapper.findByProjectId(projectId);
        for (Creation creation : creationList) {
            Integer creationId = creation.getId();
            CreationStyle creationStyle = creationStyleMapper.findByCreationId(creationId);
            if (creationStyle == null)
                continue;
            creation.setCreationStyle(creationStyle);

            CreationWordCue creationWordCue = creationWordCueMapper.findByCreationId(creationId);
            if (creationWordCue == null)
                continue;
            creation.setCreationWordCue(creationWordCue);

            CreationWordNegative creationWordNegative = creationWordNegativeMapper.findByCreationId(creationId);
            if (creationWordNegative == null)
                continue;
            creation.setCreationWordNegative(creationWordNegative);

            creation.setQueueProcess("处理中");
            CreationImgAi creationImgAi = creationImgAiMapper.findByCreationId(creationId);
            if (creationImgAi != null) {
                creation.setQueueProcess("已完成");
                String imgUrl = "/file/gen-ai-img/" + creation.getProjectId().toString() + "/" + creationImgAi.getName();
                creation.setImgUrl(imgUrl);
                creation.setCreationImgAi(creationImgAi);
            }


            if (creation.getCate() == 1) {
                CreationImgSketch creationImgSketch = creationImgSketchMapper.selectByPrimaryKey(creation.getSketchId());
                creation.setCreationImgSketch(creationImgSketch);
            }


            //进度信息

            List<TaskQueue> tasks = taskQueueMapper.findTaskByState((short) 0);
            Optional<TaskQueue> taskOp = tasks.stream().filter(p -> p.getCreationId().equals(creationId)).findFirst();
            if (taskOp.isPresent()) {
                TaskQueue task = taskOp.get();
                String queueProcess = "排队中（" + (task.getId() - tasks.get(0).getId()) + "/" + tasks.size() + ")";
                creation.setQueueProcess(queueProcess);
            }

        }
        return creationList;
    }
}
